﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using PM.Services;
using PM.Entity;

namespace PM.Site.Admin
{
    public partial class BankOverview : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserName"] == null || Session["UserTypeId"] == null)
            {
                Response.Redirect("Login.aspx?RequestUrl=" + Request.Url.OriginalString);
            }
            else if (Session["UserTypeId"] != null)
            {
                if (Int32.Parse(Session["UserTypeId"].ToString()) == 1)
                {
                    this.Loading();
                }
                else
                {
                    Response.Redirect("Default.aspx?RequestUrl=" + Request.Url.OriginalString);
                }

            }
            else
            {
                Response.Redirect("Default.aspx?RequestUrl=" + Request.Url.OriginalString);
            }
        }

        private void Loading()
        {

            if (!Page.IsPostBack)
            {
                if (Session["BankId"] != null && Session["BankId"].ToString() != string.Empty)
                {
                    Bank bank = BankServices.GetBankById(Session["BankId"].ToString());
                    txbBankId.Text = bank.BankId;
                    txbName.Text = bank.Name;
                    txbBranch.Text = bank.Branch;                    
                }
                dlBanks.DataSource = BankServices.GetBankCollection();
                dlBanks.DataBind();

            }
        }

        protected void dlBanks_ItemCommand(object source, DataListCommandEventArgs e)
        {
            string bankId = e.CommandArgument.ToString();                      
            switch (e.CommandName)
            {
                case "EditBank":
                    Session["BankId"] = bankId;
                    Response.Redirect("~/Admin/BankOverview.aspx");
                    break;
                case "DeleteBank":
                    BankServices.Delete(bankId);
                    Response.Redirect("~/Admin/BankOverview.aspx");
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
           
            Bank bank = new Bank();
            bank.BankId = txbBankId.Text;
            bank.Name = txbName.Text;
            bank.Branch = txbBranch.Text;
            if (Session["BankId"] != null || Session["BankId"].ToString() == txbBankId.Text)
            {
                BankServices.Update(bank);
                Session["BankId"] = null;
            }
            else
            {
                BankServices.Add(bank);
            }
            Response.Redirect("~/Admin/BankOverview.aspx");
        }
    }
}
